package network;

import graphics.Shape;

import java.util.List;

import network.server.ControlledShapesData;

/**
 * Message factory provides some methods to create messages. The major function
 * of this class is to help caller to create message without knowing the detail
 * of the message structure.
 * 
 * @author Jie Zhao(288654)
 * 
 */
public class MessageFactory {
	private static Message checkMessage = new Message();
	static {
		checkMessage.setType(MessageType.CHECK_ALIVE);
		checkMessage.setMessageBody("");
	}

	private MessageFactory() {
	}

	/**
	 * create a check message which is used to check if a client is still alive.
	 * 
	 * @return
	 */
	public static Message createCheckMessage() {
		return checkMessage;
	}

	/**
	 * create a login response message, which is accept or reject by the server.
	 * 
	 * @param b
	 * @return
	 */
	public static Message createLoginResp(boolean b) {
		Message m = new Message();
		m.setType(b ? MessageType.LOGIN_APPOVED : MessageType.LOGIN_REJECT);
		return m;
	}

	/**
	 * create a message which include all the drawing data on the current
	 * server.
	 * 
	 * @return
	 */
	public static Message createDrawSyncMessage() {
		Message m = new Message();
		m.setType(MessageType.DRAW_SYNC);
		StringBuffer sb = new StringBuffer();
		List<Shape> l = ControlledShapesData.getInstance().getShapeData();
		for (Shape s : l) {
			sb.append(s.toString() + "`");
		}
		if (sb.length() > 0)
			sb.deleteCharAt(sb.length() - 1);
		m.setMessageBody(sb.toString());
		return m;
	}

	/**
	 * create a synchronization request message which is used to ask the server
	 * to send currnt data.
	 * 
	 * @return
	 */
	public static Message createDrawSyncReqMessage() {
		Message m = new Message();
		m.setType(MessageType.DRAW_SYNC_REQ);
		m.setMessageBody("");
		return m;
	}

	/**
	 * create login message which include a username and password
	 * 
	 * @param username
	 * @param password
	 * @return
	 */
	public static Message createLoginMessage(String username, String password) {
		Message m = new Message();
		m.setType(MessageType.LOGIN_REQ);
		String u = (username == null ? "" : username);
		String p = (password == null ? "" : password);
		m.setMessageBody(u + ":" + p);
		return m;
	}

	/**
	 * create a clear message which is used to notify clients that the server
	 * clears current drawing.
	 * 
	 * @return
	 */
	public static Message createClearMessage() {
		Message m = new Message();
		m.setType(MessageType.CLEAR);
		return m;
	}

	/**
	 * create a drawing message which includes a string which represents a shape
	 * drew by a user
	 * 
	 * @param shape
	 * @return
	 */
	public static Message createDrawMessage(String shape) {
		Message m = new Message();
		m.setType(MessageType.DRAW);
		m.setMessageBody(shape == null ? "" : shape);
		return m;
	}
/**
 * create a chat message
 * @param sender
 * @param msg
 * @return
 */
	public static Message createChatMessage(String sender, String msg) {
		Message m = new Message();
		m.setType(MessageType.CHAT);
		m.setMessageBody(sender + ":" + msg);
		return m;
	}
}
